package com.doudou.collection.adapter.controller;

import com.doudou.collection.domain.service.AnnouncementService;
import com.doudou.collection.infrastructure.common.web.ResultVO;
import com.doudou.collection.orm.dto.AnnouncementAddNewDTO;
import com.doudou.collection.orm.dto.AnnouncementUpdateDTO;
import com.doudou.collection.orm.vo.AnnouncementVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @ClassName AnnouncementController<br>
 * @Description 公告模块<br>
 * @Author 王林峰<br>
 * @Date 2023/6/23 16:02<br>
 * @Version 1.0<br>
 */
@Api(tags="05.公告模块")
@Slf4j
@RestController
@RequestMapping("/v1/announcements")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @ApiOperation("发布公告")
    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public ResultVO addNew(AnnouncementAddNewDTO announcementAddNewDTO) {
        log.debug("开始处理【发布公告】请求: {}", announcementAddNewDTO);

        announcementService.addNew(announcementAddNewDTO);
        log.info("发布成功");
        return ResultVO.ok();
    }

    @ApiOperation("修改公告为删除状态")
    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id}/delete")
    public ResultVO enDelFlag(@PathVariable Long id) {
        log.debug("开始处理【修改公告为删除状态】请求：公告ID:{}", id);
        announcementService.delete(id);
        return ResultVO.ok();

    }


    @ApiOperation("修改公告为未删除状态")
    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperationSupport(order = 310)
    @PostMapping("/{id}/undelete")
    public ResultVO disDelFlag(@PathVariable Long id) {
        log.debug("开始处理【修改公告为未删除状态】请求：公告ID:{}", id);
        announcementService.undelete(id);
        return ResultVO.ok();

    }



    @ApiOperation("修改公告为撤回状态")
    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperationSupport(order = 320)
    @PostMapping("/{id}/cancel")
    public ResultVO cancel(@PathVariable Long id) {
        log.debug("开始处理【修改公告为撤回状态】请求：公告ID:{}", id);
        announcementService.cancel(id);
        return ResultVO.ok();

    }


    @ApiOperation("修改公告为未撤回状态")
    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperationSupport(order = 330)
    @PostMapping("/{id}/uncancel")
    public ResultVO unCancel(@PathVariable Long id) {
        log.debug("开始处理【修改公告未撤撤回状态】请求：公告ID:{}", id);
        announcementService.unCancel(id);
        return ResultVO.ok();

    }


    @ApiOperation("修改公告")
    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperationSupport(order = 340)
    @PostMapping("/{id}/update")
    public ResultVO update(AnnouncementUpdateDTO announcementUpdateDTO) {
        log.debug("开始处理【修改公告】请求：修改后公告:{}", announcementUpdateDTO);
        announcementService.updateById(announcementUpdateDTO);
        return ResultVO.ok();
    }


    @ApiOperation("查询公告")
    @PreAuthorize("hasAuthority('ADMIN')")
    @ApiOperationSupport(order = 400)
    @GetMapping("/select")
    public ResultVO selectAnnouncementAll() {
        log.debug("开始处理【查询所有公告】请求");
        List<AnnouncementVO> list = announcementService.selectAll();

        return ResultVO.ok(list);
    }









}
